package cn.bluemobi.dylan.step.presenter.my.activity;

import java.util.List;

import javax.inject.Inject;

import cn.bluemobi.dylan.step.base.RxPresenter;
import cn.bluemobi.dylan.step.moder.bean.Goods;
import cn.bluemobi.dylan.step.moder.bean.MemberAccount;
import cn.bluemobi.dylan.step.moder.bean.Score;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.moder.http.CommonSubscriber;
import cn.bluemobi.dylan.step.moder.http.RetrofitHelper;
import cn.bluemobi.dylan.step.moder.http.RxUtil;
import cn.bluemobi.dylan.step.moder.http.response.HttpResponseBean;
import cn.bluemobi.dylan.step.moder.http.response.PageDataBean;
import cn.bluemobi.dylan.step.moder.prefs.Prefs;
import cn.bluemobi.dylan.step.moder.prefs.SharedPreferencesHelper;
import cn.bluemobi.dylan.step.presenter.contract.my.IntegralContract;
import cn.bluemobi.dylan.step.presenter.contract.my.ScoreContract;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by $Liudan on 2018/8/7 0007.
 */
public class ScorePresenter extends RxPresenter<ScoreContract.View> implements ScoreContract.Presenter {
    @Inject
    SharedPreferencesHelper prefHelper;
    private int currPage = 1;
    private int pageSize = 10;


    @Inject
    public ScorePresenter() {

    }

    /**
     * 获取user
     */
    public void preUser() {
        Observable<User> observable = Observable.create(new ObservableOnSubscribe<User>() {
            @Override
            public void subscribe(ObservableEmitter<User> emitter) throws Exception {
                User user = prefHelper.getPrefObject(Prefs.key_user, User.class);
                emitter.onNext(user);
                emitter.onComplete();
            }
        });
        //创建一个下游 Observer
        Observer<User> observer = new Observer<User>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(User user) {
                if (user != null) {
                    mView.showUserInfo(user);
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        };
        //建立连接
        observable.subscribe(observer);

    }


    /**
     * 获取积分
     */
    public void getMemberAccount(int memberId) {
        addSubscribe(RetrofitHelper.getLoginApi().getMemberAccount(memberId)
                .compose(RxUtil.<HttpResponseBean<MemberAccount>>rxSchedulerHelper())
                .compose(RxUtil.<MemberAccount>handleResult())
                .subscribeWith(new CommonSubscriber<MemberAccount>(mView) {
                    @Override
                    public void onNext(MemberAccount user) {
                        mView.showMemberAccount(user);
                    }
                })
        );
    }


    /**
     * 商品
     */
    public void httpGoods() {
        addSubscribe(RetrofitHelper.getGoodsApi().getGoodsList(currPage, pageSize, 1)
                .compose(RxUtil.<HttpResponseBean<PageDataBean<List<Goods>>>>rxSchedulerHelper())
                .compose(RxUtil.<PageDataBean<List<Goods>>>handleResult())
                .subscribeWith(new CommonSubscriber<PageDataBean<List<Goods>>>(mView) {
                    @Override
                    public void onNext(PageDataBean<List<Goods>> result) {
                        mView.httpGoods(result.getRecords());
                    }
                })
        );
    }

//    /**
//     * 积分明细
//     */
//    public void httpScoreList() {
//        addSubscribe(RetrofitHelper.getLoginApi().getMemberAccountLogList(currPage, pageSize, 1)
//                .compose(RxUtil.<HttpResponseBean<PageDataBean<List<Score>>>>rxSchedulerHelper())
//                .compose(RxUtil.<PageDataBean<List<Score>>>handleResult())
//                .subscribeWith(new CommonSubscriber<PageDataBean<List<Score>>>(mView) {
//                    @Override
//                    public void onNext(PageDataBean<List<Score>> result) {
//                        mView.httpScoreList(result.getRecords());
//                    }
//                })
//        );
//    }

}
